<?php
namespace app\admin\controller;
use app\admin\model\Project;
use app\admin\model\Detail;
use app\admin\model\Grade;
use think\Db;
require_once EXTEND_PATH.'GatewayClient/Gateway.php';
use GatewayClient\Gateway;

class Program extends Base1
{
    public static $one = 1;
    /**
     * 添加比赛项目
     */
    public function addPro()
    {
        $data = input();
        $pro = new Project();
        $pro->proname = $data['game'];
        $pro->performer = $data['per'];
        $pro->musical = $data['tool'];
        $list = $pro->save();
        if($list) {
            return ['status'=>1,'msg'=>'添加成功'];
        }
    }

    /**
     * 修改比赛项目1
     */
    public function selPro()
    {
        $data =  input();
        $pro = new Project();
        $list = $pro->where('id',$data['nowId'])->find();
        if($list) {
            return ['status'=>1,'data'=>$list];
        }
    }

    /**
     * 修改比赛项目2
     */
    public function modPro()
    {
        $data = input();
        $tmp['id'] = $data['nowId'];
        $tmp['performer'] = $data['per1'];
        $tmp['proname'] = $data['game1'];
        $tmp['musical'] = $data['tool1'];
        $pro = new Project();
        $pro->update($tmp);
        return ['status'=>1,'msg'=>'修改成功'];
    }

    /**
     * 删除项目
     */
    public function delPro()
    {
        $data = input();
        $list = Db::name('project')->where('id',$data['nowId'])->update(['enable'=>1]);
        if($list) {
            return ['status'=>1,'msg'=>'删除成功'];
        }
    }


    /**
     * 比赛开始（改变状态）
     */
    public function start()
    {
        $data = input();
        $pro = new Project();
        $time = date('Y-m-d H:i:s',time());
        $list = $pro->where('id',$data['nowId'])->update(['status'=>2,'update_time'=>$time]);
        $rel = $pro->where('id',$data['nowId'])->find();
        Db::name('sign')->where('song',$rel['proname'])->update(['enable'=>1]);
        $data['type'] = 'msg';
        $data['data'] = '刷新';
        Gateway::sendToGroup(2,json_encode($data));
        if($list) {
            return ['status'=>1,'msg'=>'比赛开始'];
        }
    }

    /**
     * 开始比赛
     */
    public function running()
    {
        $data = input();
        $client_id = $data['client_id'];
        Gateway::joinGroup($client_id,self::$one);
        return $this->returnOk();
    }

    public function send($score = null)
    {
        $data['type'] = 'msg';
        $data['data'] = $score;
        Gateway::sendToGroup(self::$one,json_encode($data));
    }

    /**
     * 客户端绑定
     */
    public function bind()
    {
        $data = input();
        $client_id = $data['client_id'];
        Gateway::joinGroup($client_id,2);
        return $this->returnOk();
    }

    /**
     * 结束比赛
     */
    public function end()
    {
        $data = input();
        $pro = new Project();
        $pro->where('id',$data['id'])->update(['status'=>3]);
        //解封评分状态
        $del = new Detail();
        $del->where('pid',$data['id'])->update(['enable'=>2]);
        $rel = $this->avg($data['id']);
        $rel['pid'] = $data['id'];
        $list = Db::name('grade')->insert($rel);
        if($list) {
            return ['status'=>1];
        }
    }

    /**
     * 计算评分
     */
    public function avg($id)
    {
        $del = new Detail();
        //计算参赛选手平均分
        $rel1 = $del->where('pid',$id)->where('enable',2)->select();
        if(!empty($rel1)) {
            $learn = [];
            $skill = [];
            $fluency = [];
            $control = [];
            $influence = [];
            foreach($rel1 as $val) {
                $learn[] = $val['learn'];
                $skill[] = $val['skill'];
                $fluency[] = $val['fluency'];
                $control[] = $val['control'];
                $influence[] = $val['influence'];
            }
            $num = count($learn);
            if($num == 1) {
                $rel['learn'] = array_sum($learn);
                $rel['skill'] = array_sum($skill);
                $rel['fluency'] = array_sum($fluency);
                $rel['control'] = array_sum($control);
                $rel['influence'] = array_sum($influence);
            }
            if($num == 2) {
                $rel['learn'] = array_sum($learn) / 2;
                $rel['skill'] = array_sum($skill) / 2;
                $rel['fluency'] = array_sum($fluency) / 2;
                $rel['control'] = array_sum($control) / 2;
                $rel['influence'] = array_sum($influence) / 2;
            }
            if($num >= 3) {
                sort($learn);
                sort($skill);
                sort($fluency);
                sort($control);
                sort($influence);
                $rel['learn'] = array_sum(array_slice($learn,1,$num-2)) / ($num-2);
                $rel['skill'] = array_sum(array_slice($skill,1,$num-2)) / ($num-2);
                $rel['fluency'] = array_sum(array_slice($fluency,1,$num-2)) / ($num-2);
                $rel['control'] = array_sum(array_slice($control,1,$num-2)) / ($num-2);
                $rel['influence'] = array_sum(array_slice($influence,1,$num-2)) / ($num-2);
            }
            $rel['avgscore'] = ($rel['learn'] + $rel['skill'] + $rel['fluency'] + $rel['control'] + $rel['influence']);
        }else {
            $rel['learn'] = 0;
            $rel['skill'] = 0;
            $rel['fluency'] = 0;
            $rel['control'] = 0;
            $rel['influence'] = 0;
            $rel['avgscore'] = 0;
        }

        return $rel;
    }

    /**
     * 删除比赛记录
     */
    public function delHistory()
    {
        $data = input();
        $list = Db::name('grade')->where('pid',$data['nowId'])->update(['enable'=>1]);
        if($list) {
            return ['status'=>1,'msg'=>'删除成功'];
        }
    }
	/**
     * 删除参赛人员
     */
    public function delSign()
    {
        $data = input();
        $list = Db::name('sign')->where('id',$data['nowId'])->update(['enable'=>1]);
        if($list) {
            return ['status'=>1,'msg'=>'删除成功'];
        }
    }
}